import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-10-22
 * Time:9:43
 */
public class TestBInaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }

    }
    public TreeNode create(){
        TreeNode nodeA=new TreeNode('A');
        TreeNode nodeB=new TreeNode('B');
        TreeNode nodeC=new TreeNode('C');
        TreeNode nodeD=new TreeNode('D');
        TreeNode nodeE=new TreeNode('E');
        TreeNode nodeF=new TreeNode('F');
        TreeNode nodeG=new TreeNode('G');
        TreeNode nodeH=new TreeNode('H');
        nodeA.left=nodeB;
        nodeA.right=nodeC;
        nodeB.left=nodeD;
        nodeB.right=nodeE;
        nodeC.left=nodeF;
        nodeC.right=nodeG;
        nodeE.right=nodeH;
        return nodeA;
    }
    //反转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode cur=root.left;
        root.left=root.right;
        root.right=cur;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    //判断是不是对称的二叉树
    Queue<TreeNode> queue=new LinkedList<>();
    public boolean isCompara(TreeNode left,TreeNode right){
        if(left==null&&right==null){
            return true;
        }
        if(left==null||right==null||right.val!=left.val){
            return false;
        }
        return isCompara(right.right,left.left)&&isCompara(right.left,left.right);
    }
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return isCompara(root.left,root.right);
    }
    //判断是不是高度平衡的二叉树
    public boolean isBalanced(TreeNode root) {
        if(root==null){
            return true;
        }
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        int height=left>right?left-right:right-left;
        if(height<1){
            return isBalanced(root.left)&&isBalanced(root.right);
        }
        return false;
    }
    //获取最大的高度
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        return left>right?left+1:right+1;
    }
}
